package hxg.thread.lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author HeXiangGuang
 * @create 2021-06-24 17:41
 */
public class Ticket implements Runnable{
    private int ticket  = 100;
    private Lock lock = new ReentrantLock();

    @Override
    public void run() {
        while (true){
            lock.lock();
            try {
                if (ticket <= 0){
                    break;
                }
                System.out.println(Thread.currentThread().getName() +"买了第"+ticket+"张票");
                ticket --;
            } finally {
                lock.unlock();
            }
        }
    }
}
